package com.zut.controller;

import com.alibaba.druid.filter.config.ConfigTools;
import com.github.pagehelper.PageInfo;
import com.zut.domain.Student;
import com.zut.service.StudentService;
import com.zut.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /*学员注册*/
    @PostMapping("/register")
    public Result save(@RequestBody Student student) throws Exception {
        Student student1 = studentService.selectById(student.getUserId());
        int status = 0;
        if (student1 != null) {
            status = 1;
        }
        if (status == 1) {
            return new Result(Code.SAVE_ERR, null, "用户已存在！");
        } else {
            String password = student.getPassword();
            student.setPassword(ConfigTools.encrypt(password));
            studentService.insert(student);
            return new Result(Code.SAVE_OK, null, "注册成功！");
        }
    }

    /*学员登录*/
    @PostMapping(value = "/login")
    public Result getUserLogin(@RequestBody Student student) throws Exception {
        String password = student.getPassword();
        student.setPassword(ConfigTools.encrypt(password));
        Student student1 = studentService.getByNP(student);
        if (student1 != null) {
            String token = JwtUtil.sign(student.getUserId(), student.getPassword());
            HashMap users = new HashMap();
            users.put("token", token);
            users.put("id", student.getUserId());
            return new Result(Code.GET_OK, users, "登录成功！");
        } else {
            return new Result(Code.GET_ERR, "用户名或密码错误！");
        }
    }

    /*得到所有学员信息*/
    @GetMapping("/{pageNum}/{pageSize}")
    private Result getAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<Map<String, String>> students = studentService.getAll(pageNum, pageSize);
        Integer code = students.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = students.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, students, msg);
    }

    /*根据id得到学员信息*/
    @GetMapping("/{id}")
    public Result SelectById(@PathVariable String id) {
        Map<String, String> student = studentService.getById(id);
        Integer code = student != null ? Code.GET_OK : Code.GET_ERR;
        String msg = student != null ? "" : "数据查询失败，请重试！";
        return new Result(code, student, msg);
    }

    /*根据id删除学员信息*/
    @DeleteMapping("/{id}")
    public Result DeleteById(@PathVariable String id) {
        boolean flag = studentService.deleteById(id);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    /*修改学员信息*/
    @PutMapping
    public Result Update(@RequestBody Student student) throws Exception {
        String password = student.getPassword();
        if (password != null) {
            student.setPassword(ConfigTools.encrypt(password));
        }
        boolean flag = studentService.update(student);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    /*根据教练id查询学员信息*/
    @GetMapping("/{pageNum}/{pageSize}/{id}")
    public Result SelectTest(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable String id) {
        PageInfo<Map<String, String>> students = studentService.getByCoachId(pageNum, pageSize, id);
        Integer code = students.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = students.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, students, msg);
    }


    /*分页测试*/
    @GetMapping("/test/{pageNum}/{pageSize}")
    public Result SelectTest(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<Map<String, String>> students = studentService.safePage(pageNum, pageSize);
        Integer code = students.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = students.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, students, msg);
    }

    /*查询优秀学员*/
    @GetMapping("/best")
    public Result getBestStudent() {
        List<Student> students = studentService.getBestStudent();
        Integer code = students != null ? Code.GET_OK : Code.GET_ERR;
        String msg = students != null ? "" : "数据查询失败，请重试！";
        return new Result(code, students, msg);
    }

    /*    更换密码,json传输数据
        {
            "id":"123",
                "oldPassword":"123",
                "newPassword":"0987"
        }*/
    @PutMapping("/update")
    public Result updatePwd(@RequestBody HashMap<String, String> map) throws Exception {
        String id = map.get("id");
        String oldPassword = map.get("oldPassword");
        String newPassword = map.get("newPassword");
        String password = studentService.getPassword(id);
        String dePwd = ConfigTools.decrypt(password);
        String enPwd = ConfigTools.encrypt(newPassword);
        if (dePwd.equals(oldPassword)) {
            map.replace("newPassword", enPwd);
            boolean flag = studentService.updatePassword(map);
            return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag, "密码修改成功");
        } else {
            return new Result(Code.UPDATE_ERR, "密码输入错误");

        }
    }

}
